﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mysoft.MyCM.Business
{
	/// <summary>
	/// CMLog调用日志记录
	/// </summary>
	public static class CMLogger
	{/// <summary>
		/// 以消息级别记录
		/// </summary>
		/// <param name="fromApp"></param>
		/// <param name="fromUser"></param>
		/// <param name="fromIp"></param>
		/// <param name="verb"></param>
		/// <param name="url"></param>
		/// <param name="message"></param>
		public static void Info(string fromApp, string fromUser, string fromIp, string verb, string url, string message)
		{
			Log(fromApp, fromUser, fromIp, verb, url, message, M.MExceptionLevel.Message);
		}
		/// <summary>
		/// 以消息级别记录
		/// </summary>
		/// <param name="log"></param>
		public static void Info(CMLog log)
		{
			Log(log, M.MExceptionLevel.Message);
		}
		/// <summary>
		/// 以警告级别记录
		/// </summary>
		/// <param name="fromApp"></param>
		/// <param name="fromUser"></param>
		/// <param name="fromIp"></param>
		/// <param name="verb"></param>
		/// <param name="url"></param>
		/// <param name="message"></param>
		public static void Warning(string fromApp, string fromUser, string fromIp, string verb, string url, string message)
		{
			Log(fromApp, fromUser, fromIp, verb, url, message, M.MExceptionLevel.Warning);
		}
		/// <summary>
		/// 以警告级别记录
		/// </summary>
		/// <param name="log"></param>
		public static void Warning(CMLog log)
		{
			Log(log, M.MExceptionLevel.Warning);
		}
		/// <summary>
		/// 以终止级别记录
		/// </summary>
		/// <param name="fromApp"></param>
		/// <param name="fromUser"></param>
		/// <param name="fromIp"></param>
		/// <param name="verb"></param>
		/// <param name="url"></param>
		/// <param name="message"></param>
		public static void Stop(string fromApp, string fromUser, string fromIp, string verb, string url, string message)
		{
			Log(fromApp, fromUser, fromIp, verb, url, message, M.MExceptionLevel.Stop);
		}
		/// <summary>
		/// 以终止级别记录
		/// </summary>
		/// <param name="log"></param>
		public static void Stop(CMLog log)
		{
			Log(log, M.MExceptionLevel.Stop);
		}
		/// <summary>
		/// 以致命级别记录
		/// </summary>
		/// <param name="fromApp"></param>
		/// <param name="fromUser"></param>
		/// <param name="fromIp"></param>
		/// <param name="verb"></param>
		/// <param name="url"></param>
		/// <param name="message"></param>
		public static void Fatal(string fromApp, string fromUser, string fromIp, string verb, string url, string message)
		{
			Log(fromApp, fromUser, fromIp, verb, url, message, M.MExceptionLevel.Fatal);
		}
		/// <summary>
		/// 以致命级别记录
		/// </summary>
		/// <param name="log"></param>
		public static void Fatal(CMLog log)
		{
			Log(log, M.MExceptionLevel.Fatal);
		}

		/// <summary>
		/// 记录日志
		/// </summary>
		/// <param name="fromApp"></param>
		/// <param name="fromUser"></param>
		/// <param name="fromIp"></param>
		/// <param name="verb"></param>
		/// <param name="url"></param>
		/// <param name="message"></param>
		/// <param name="level"></param>
		public static void Log(string fromApp, string fromUser, string fromIp, string verb, string url, string message, M.MExceptionLevel level)
		{
			CMLog log = new CMLog()
			{
				FromApp = fromApp,
				FromUser = fromUser,
				FromIP = fromIp,
				CalledVerb = verb,
				CalledUrl = url,
				Message = message
			};
			Log(log, level);
		}
		/// <summary>
		/// 记录日志
		/// </summary>
		/// <param name="log"></param>
		/// <param name="level"></param>
		public static void Log(CMLog log, M.MExceptionLevel level)
		{
			innerLog(log, level);
		}
		/// <summary>
		/// 记录日志
		/// </summary>
		/// <param name="log"></param>
		private static void innerLog(CMLog log, M.MExceptionLevel level)
		{
			try
			{
				if (log == null) return;
				// 处理下默认数据
				if (string.IsNullOrEmpty(log.FromUser)) log.FromUser = "unknown";
				log.Id = Guid.NewGuid();
				log.CreateOn = DateTime.Now;
				log.Level = level.ToString("G");
				// 异步保存Log，避免系统阻塞
				Task tLog = new Task(
					new Action(() =>
					{
						try
						{
							using (var db = new LogModel())
							{
								db.CMLog.Add(log);
								// 不检查保存结果
								db.SaveChanges();
							}
						}
						catch { }
					}));
				tLog.Start();
			}
			catch { }
		}
	}
}

